package com.pay.cloud.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.pay.cloud.BindState;
import com.pay.cloud.DataFactory;
import com.pay.cloud.R;
import com.pay.cloud.cpay.MyCloudPay;
import com.pay.cloud.cpay.OrderStatClientInfo;
import com.pay.cloud.cpay.QueryOrderListOverviewRequest;
import com.pay.cloud.cpay.QueryOrderListOverviewResponse;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

public class StatisticsOverviewFragment extends AbstractNetworkFragment implements BindState {
    private static final String TAG = StatisticsOverviewFragment.class.getSimpleName();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_statistics_overview, container, false);
    }

    @Override
    protected void execute() throws Exception {
        showLoading();
        QueryOrderListOverviewRequest req = new QueryOrderListOverviewRequest();
        req.setSub_pay_platforms(new int[]{100, 200});
        QueryOrderListOverviewResponse res = MyCloudPay.getInstance(getActivity()).queryOrderListOverview(req);
        handle(() -> {
            try {
                showData(res.getOverviews());
                hideLoading();
            } catch (Exception e) {
                Log.e(TAG, "showData: ", e);
                onError(e.getMessage());
            }
        });

    }

    @SuppressLint("DefaultLocale")
    private void showData(List<OrderStatClientInfo> list) {
        long refundAmount = 0;
        long refundCount = 0;
        long successAmount = 0;
        long successCount = 0;
        long wxRefundAmount = 0;
        long wxRefundCount = 0;
        long wxSuccessAmount = 0;
        long wxSuccessCount = 0;
        long aliRefundAmount = 0;
        long aliRefundCount = 0;
        long aliSuccessAmount = 0;
        long aliSuccessCount = 0;
        long othRefundAmount = 0;
        long othRefundCount = 0;
        long othSuccessAmount = 0;
        long othSuccessCount = 0;
        if (null != list && list.size() > 0) {
            for (OrderStatClientInfo info: list) {
                refundAmount += info.getRefund_create_amount();
                refundCount += info.getRefund_create_count();
                successAmount += info.getSuccess_amount();
                successCount += info.getSuccess_count();
                if (info.getSub_pay_platform() == 100) {
                    wxRefundAmount += info.getRefund_create_amount();
                    wxRefundCount += info.getRefund_create_count();
                    wxSuccessAmount += info.getSuccess_amount();
                    wxSuccessCount += info.getSuccess_count();
                } else if (info.getSub_pay_platform() == 200) {
                    aliRefundAmount += info.getRefund_create_amount();
                    aliRefundCount += info.getRefund_create_count();
                    aliSuccessAmount += info.getSuccess_amount();
                    aliSuccessCount += info.getSuccess_count();
                } else {
                    othRefundAmount += info.getRefund_create_amount();
                    othRefundCount += info.getRefund_create_count();
                    othSuccessAmount += info.getSuccess_amount();
                    othSuccessCount += info.getSuccess_count();
                }
            }
        }
        String separator = System.getProperty("line.separator");
        TextView textView = getActivity().findViewById(R.id.stat_container);
        textView.setText(String.format("收款：%d笔，%s元 %s微信：%d笔，%s元 %s支付宝：%d笔，%s元" +
                        "%s------------------------------------------------------%s" +
                        "退款：%d笔，%s元 %s微信：%d笔，%s元 %s支付宝：%d笔，%s元",
                successCount, scale(successAmount),
                separator,
                wxSuccessCount, scale(wxSuccessAmount),
                separator,
                aliSuccessCount, scale(aliSuccessAmount),
                separator,
                separator,
                refundCount, scale(refundAmount),
                separator,
                wxRefundCount, scale(wxRefundAmount),
                separator,
                aliRefundCount, scale(aliRefundAmount)
                ));
    }

    private BigDecimal scale(long amount) {
        return new BigDecimal("" + amount).divide(new BigDecimal("100")).setScale(2, RoundingMode.DOWN);
    }
}
